package com.mole.rbac.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
@CrossOrigin // 解决跨域问题
public class StudentController {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	@GetMapping("list")
	public List<Map<String,Object>> list() {
		return jdbcTemplate.queryForList("select * from t_student");
	}
	
	@PostMapping("add")
	public int add(Student student) {
		int result = -1;
		String name = student.getName();
		int age = student.getAge();
		int sex = student.getSex();
		String sql = "INSERT INTO t_student(name, age, sex) VALUES ('" + name + "', '" + age + "', '" + sex + "')";
		try {
			result = jdbcTemplate.update(sql);
			return result;
		} catch(DataAccessException e) {
			e.printStackTrace();
			return result;
		}
		
	}
	
	@DeleteMapping("delete")
	public int deleteById(String id) {
		int result = -1;
		String sql = "DELETE FROM t_student WHERE id = '" + id + "'";
		try {
			result = jdbcTemplate.update(sql);
			return result;
		} catch(DataAccessException e) {
			e.printStackTrace();
			return result;
		}
	}
	
	@PostMapping("update")
	public int updateById(Student student) {
		int result = -1;
		String id = student.getId();
		if(searchById(id) != null) {
			String name = student.getName();
			int age = student.getAge();
			int sex = student.getSex();
			String sql = "UPDATE t_student SET name = '" + name + "', age = '" + age + "', sex = '" + sex + "' WHERE id = '" + id + "'";
			try {
				result = jdbcTemplate.update(sql);
				return result;
			} catch(DataAccessException e) {
				e.printStackTrace();
				return result;
			}
		}
		return result;
	}
	
	@GetMapping("search")
	public Student searchById(@RequestParam(name = "id") String id) {
		String sql = "select * from t_student where id = '" + id + "'";
		System.out.println(id);
		Map<String,Object> map = new HashMap<>();
		
		try {
			map = jdbcTemplate.queryForMap(sql);
			
			String id1 = map.get("id").toString();
			String name = map.get("name").toString();
			String age = map.get("age").toString();
			String sex = map.get("sex").toString();
			return new Student(id1, name, Integer.parseInt(age), Integer.parseInt(sex));
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
}

class Student {
	private String id;
	private String name;
	private int age;
	private int sex;
	
	public Student() {
		super();
	}
	public Student(String id, String name, int age, int sex) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
}
